Home > sgwt_toolbox > demo > sgwt_demo2.m

sgwt_demo2

PURPOSE ^

sgwt_demo2 : Allows exploring wavelet scale and approximation accuracy

SYNOPSIS ^

function sgwt_demo2

DESCRIPTION ^

 sgwt_demo2 : Allows exploring wavelet scale and approximation accuracy

 This demo builds the SGWT for the minnesota traffic graph, a graph
 representing the connectivity of the minnesota highway system. One center
 vertex is chosen, and then the exact (naive forward transform) and the
 approximate (via chebyshev polynomial approximation) wavelet transforms
 are computed for a particular value of the wavelet scale t. The relative
 error of the exact and approximate wavelets is computed. The user may
 then adjust the value of t, the degree m of the chebyshev polynomial
 approximation, and the center vertex in order to explore their effects.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % sgwt_demo2 : Allows exploring wavelet scale and approximation accuracy
0002 %
0003 % This demo builds the SGWT for the minnesota traffic graph, a graph
0004 % representing the connectivity of the minnesota highway system. One center
0005 % vertex is chosen, and then the exact (naive forward transform) and the
0006 % approximate (via chebyshev polynomial approximation) wavelet transforms
0007 % are computed for a particular value of the wavelet scale t. The relative
0008 % error of the exact and approximate wavelets is computed. The user may
0009 % then adjust the value of t, the degree m of the chebyshev polynomial
0010 % approximation, and the center vertex in order to explore their effects.
0011 
0012 % This file is part of the SGWT toolbox (Spectral Graph Wavelet Transform toolbox)
0013 % Copyright (C) 2010, David K. Hammond.
0014 %
0015 % The SGWT toolbox is free software: you can redistribute it and/or modify
0016 % it under the terms of the GNU General Public License as published by
0017 % the Free Software Foundation, either version 3 of the License, or
0018 % (at your option) any later version.
0019 %
0020 % The SGWT toolbox is distributed in the hope that it will be useful,
0021 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0022 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0023 % GNU General Public License for more details.
0024 %
0025 % You should have received a copy of the GNU General Public License
0026 % along with the SGWT toolbox.  If not, see <http://www.gnu.org/licenses/>.
0027 
0028 function sgwt_demo2
0029 close all
0030 fprintf('Welcome to SGWT demo #2\n');
0031 
0032 % touch variables to be shared among sub-functions
0033 gb=[]; c=[]; 
0034 
0035 % create UI elements
0036 fh=figure('Visible','on','Name','demo 2 ui','Position',[425,920,400,150]);
0037 uipanelh=uipanel('Parent',fh,'Title','','Units','pixels','BorderType','none');
0038 tsliderh=uicontrol(uipanelh,'style','slider','max',50,'min',0,'value',1,...
0039                    'sliderstep',[.005 .1],'position',[25,10,300,20],...
0040                    'callback',{@tslider_callback});
0041 msliderh=uicontrol(uipanelh,'style','slider','max',100,'min',1,'value',20,...
0042                    'sliderstep',[.001 .1],'position',[25,60,300,20],...
0043                    'callback',{@mslider_callback});
0044 jbuttonh=uicontrol(uipanelh,'style','pushbutton','position',[50,110,150,20],...
0045                    'string','Select center vertex','callback',{@jbutton_callback});
0046 ttexth=uicontrol(uipanelh,'style','text','string','','position',[325,10,100,20]);
0047 mtexth=uicontrol(uipanelh,'style','text','string','','position',[325,60,100,20]);
0048 jtexth=uicontrol(uipanelh,'style','text','string','','position',[325,100,100,20]);
0049 uicontrol(uipanelh,'style','text','string',...
0050           'Chebyshev polynomial order (m)','position',...
0051            [60,80,200,20]);
0052 uicontrol(uipanelh,'style','text','string',...
0053           'Wavelet scale (t)','position',...
0054           [60,30,200,20]);
0055 
0056 
0057 %% Load graph and compute Laplacian
0058 fprintf('Loading minnesota traffic graph\n');
0059 Q=load('minnesota.mat');
0060 xy=Q.xy;
0061 A=Q.A;
0062 N=size(A,1);
0063 x=xy(:,1);
0064 y=xy(:,2);
0065 
0066 fprintf('Computing graph laplacian\n')
0067 [ki,kj]=find(A);
0068 L=sgwt_laplacian(A);
0069 fprintf('Measuring largest eigenvalue, lmax = ');
0070 lmax=sgwt_rough_lmax(L);
0071 fprintf('%g\n',lmax);
0072 arange=[0 lmax];
0073 
0074 msize=100;
0075 
0076 % initial values
0077 t=3; % wavelet scale
0078 
0079 m=20; % chebyshev polynomial order, for approximation
0080 jcenter=550;
0081 
0082 fprintf('\n');
0083 update_uitext;
0084 update_graphfig
0085 update_kernel
0086 update_waveletfigs
0087 
0088 function update_graphfig
0089   figure(2)
0090   set(gcf,'renderer','zbuffer');
0091   fprintf('Displaying traffic graph\n');
0092   set(gcf,'position',[0,600,400,400]);
0093   %clf('reset');
0094   hold on
0095   scatter(x,y,msize,[.5 .5 .5],'.');
0096   plot([x(ki)';x(kj)'],[y(ki)';y(kj)'],'k');
0097   set(gca,'Xtick',[]);
0098   set(gca,'Ytick',[]);
0099   axis equal
0100   axis off
0101   scatter(x(jcenter),y(jcenter),msize,'r.');
0102   drawnow
0103   end
0104 
0105   function update_kernel
0106   % select wavelet kernel
0107   t1=1;
0108   t2=2;
0109   a=2;
0110   b=2;
0111   tmin=t1/lmax; 
0112 % scales t<tmin will show same wavelet shape as t=tmin, as
0113 % wavelet kernel g is monomial in interval [0,1)
0114 set(tsliderh,'min',tmin);
0115   gb= @(x) sgwt_kernel(x,'a',a,'b',b,'t1',t1,'t2',t2);
0116   g=@(x) gb(t*x);
0117   % polynomial approximation
0118   for k=1:numel(g)
0119     c=sgwt_cheby_coeff(g,m,m+1,arange);
0120   end
0121   lambda=linspace(0,lmax,1e3);
0122   figure(3)
0123   set(gcf,'position',[425,580,600,250])
0124   plot(lambda,g(lambda),lambda,sgwt_cheby_eval(lambda,c,arange));
0125   legend('Exact Wavelet kernel','Chebyshev polynomial approximation');
0126 end
0127 
0128 function update_waveletfigs
0129   
0130   fprintf('\nReomputing wavelets with t=%g, m=%g\n',t,m);
0131   d=sgwt_delta(N,jcenter);
0132   fprintf('Computing wavelet by naive forward transform\n');
0133   figure(4)
0134   set(gcf,'position',[0,100,400,400])
0135   wp_e=sgwt_ftsd(d,gb,t,L);
0136   show_wavelet(wp_e,x,y);
0137   % show wavelet (naive)
0138   title('exact wavelet (naive forward transform)');  
0139   fprintf('Computing wavelet by Chebyshev approximation\n');
0140   figure(5)
0141   set(gcf,'position',[425,100,400,400])
0142   % show wavelet (chebyshev)
0143   wp_c=sgwt_cheby_op(d,L,c,arange);
0144   show_wavelet(wp_c,x,y);
0145   title('approximate wavelet (transform via chebyshev approximation)');
0146   relerr=norm(wp_e-wp_c)/norm(wp_e);
0147   fprintf('Relative error between exact and approximate wavelet %g\n',relerr)
0148 end
0149 
0150 function show_wavelet(wp,x,y)
0151 [Fs,s_ind]=sort(abs(wp),'descend');
0152 scatter(x(s_ind),y(s_ind),msize,wp(s_ind),'.');
0153 caxis([-1 1]*max(abs(wp)));
0154 hcb=colorbar('location','north');
0155 set(gca,'Xtick',[]);
0156 set(gca,'Ytick',[]);
0157 cxt=get(hcb,'Xtick');
0158 cxt=[cxt(1),0,cxt(end)];
0159 set(hcb,'Xtick',cxt);
0160 cpos=get(hcb,'Position');
0161 cpos(4)=.02; % make colorbar thinner
0162 set(hcb,'Position',cpos);
0163 axis equal
0164 axis off
0165 end
0166 
0167 function update_uitext
0168 set(ttexth,'string',sprintf('t=%0.3f',t));
0169 set(mtexth,'string',sprintf('m=%g',m));
0170 set(jtexth,'string',sprintf('j=%g',jcenter));
0171 end
0172 
0173 function tslider_callback(source,eventdata)
0174 t=get(tsliderh,'value');
0175 update_uitext;
0176 update_kernel;
0177 update_waveletfigs;
0178 end
0179 
0180 function mslider_callback(source,eventdata)
0181 newm=get(msliderh,'value');
0182 if newm<m
0183   m=floor(newm);
0184 else
0185   m=ceil(newm);
0186 end
0187 set(msliderh,'value',m);
0188 update_uitext;
0189 update_kernel;
0190 update_waveletfigs;
0191 end
0192 
0193 function jbutton_callback(source,eventdata)
0194 figure(2)
0195 fprintf('Select new center vertex\n');
0196 [xp,yp]=ginput(1);
0197 oldjcenter=jcenter;
0198 jcenter=argmin((xp-x).^2+(yp-y).^2);
0199 scatter(x(jcenter),y(jcenter),msize,'r.');
0200 scatter(x(oldjcenter),y(oldjcenter),msize,[.5 .5 .5],'.');
0201 drawnow
0202 update_uitext
0203 update_waveletfigs
0204 end
0205 
0206 end
0207 
0208 function i = argmin(x)
0209   i=min(find(x==min(x)));
0210 end

Generated on Wed 13-Oct-2010 13:36:39 by m2html © 2003